package yz.com.javautil.test;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liuxk
 * @since 2021-06-18
 * 日期格式工具
 */
public class DateFormat {
    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM/dd/yy");

    /**
     * 返回当前系统时间  格式 2013-05-13 11:43:27
     */
    public static String getDate() {
        String date = "";
        date = simpleDateFormat.format(new Date());
        return date;
    }

    public static String getDate2() {
        String date = "";
        date = simpleDateFormat2.format(new Date());
        return date;
    }

    public static String getDate(long date) {
        String date1 = "";
        date1 = simpleDateFormat.format(date);
        return date1;
    }

    /**
     * 将 2013-05-13 11:47:24 转换成 long 型
     */
    public static long getLongDate(String date) {
        long dateLong = -1;
        try {
            Date date2 = simpleDateFormat.parse(date);
            dateLong = date2.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return dateLong;
    }

    public static Date getDateByStr(SimpleDateFormat sdf, String date) {
        Date date2 = null;
        try {
            date2 = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date2;
    }

    public static void main(String[] atgs) {
//        System.out.println(getLongDate("2013-05-13 11:47:24"));
        test("2022-06");
    }

    public static String[] test(String month) {
        String[] timeMinAndMax = new String[2];

        String formatStr = "yyyy-MM-dd";
        DateTime parse = DateUtil.parse(month + "-01", formatStr);
        String dayStr = DateUtil.format(DateUtil.endOfMonth(parse), formatStr);

        timeMinAndMax[0] = dayStr.substring(0, 8) + "01 00:00:00";
        timeMinAndMax[1] = dayStr + " 23:59:59";
        System.out.println(timeMinAndMax[0] + "-" + timeMinAndMax[1]);

        String lastMonth = DateUtil.format(DateUtils.addMonths(parse, -1), "yyyy-MM");
        String lastLastMonth = DateUtil.format(DateUtils.addMonths(parse, -2), "yyyy-MM");

        String collect = Stream.of(month, lastMonth, lastLastMonth).collect(Collectors.joining("','", "'", "'"));
        System.out.println(collect);
        return timeMinAndMax;
    }
}
